#!/usr/bin/env bash
#########################################################################
# File Name   : nova.sh.j2
# Author      : seadog
# mail        : seadog0514@163.com
# Version     : 0.1
# Created Time: Mon 2024-09-30 13:17:38(+0800)
#########################################################################

#
# Usage:

{
  set -euo pipefail
}
source /root/keystonerc

# !!! u22 image
# if [[ ! -f /root/ubuntu-22.04-server-cloudimg-amd64.img ]]; then
#   cd /root && wget http://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img
# fi
# openstack --insecure image create "Ubuntu2204" --file /root/ubuntu-22.04-server-cloudimg-amd64.img --disk-format qcow2 --container-format bare --public

# !!! cirros image
if [[ ! -f /root/cirros-0.6.3-x86_64-disk.img ]]; then
  cd /root && wget http://download.cirros-cloud.net/0.6.3/cirros-0.6.3-x86_64-disk.img
fi
openstack --insecure image create cirros-0.6.3 --file /root/cirros-0.6.3-x86_64-disk.img --disk-format qcow2 --container-format bare --public

# !!! flavor
openstack --insecure flavor create --id 1 --vcpus 1 --ram 256 --disk 4 m1.tiny
openstack --insecure flavor create --id 2 --vcpus 1 --ram 1024 --disk 10 m1.small
openstack --insecure flavor create --id 3 --vcpus 2 --ram 4096 --disk 12 --ephemeral 10 m1.medium
openstack --insecure flavor create --id 4 --vcpus 4 --ram 8192 --disk 12 --ephemeral 10 m1.large

# openstack --insecure flavor list

# !!! security group
# create a security group for instances
security_group_name="secgroup01"
openstack security group create ${security_group_name}
openstack security group rule create --protocol any --remote-ip 0.0.0.0/0 --ingress ${security_group_name}
# openstack security group rule create --protocol any --remote-ip 0.0.0.0/0 --egress ${security_group_name}
# openstack security group rule create --protocol icmp --remote-ip 0.0.0.0/0 --ingress ${security_group_name}
# openstack security group rule create --protocol icmp --remote-ip 0.0.0.0/0 --egress ${security_group_name}
# openstack security group rule create --protocol icmp --remote-ip 0.0.0.0/0 --ingress ${security_group_name}
# openstack security group rule list ${security_group_name}
#
# openstack security group rule create default --remote-group default --protocol icmp
# openstack security group rule create default --remote-group default --dst-port 22

# !!! public-key
echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCo7ni6rb1+nNIN+IAE9tK+TSW+4z0R1yx5HUIXUVWRbEEe8HeN+OyMNmoNylyUofgw+BneXFy/p+eouM0F+qediarKJ97kB8XOp+u4PlTaBmtpdRiW3pVXnqnDskMaiq64Dn+XfrKgfZEDzTjsydVvEER7s+9tLvAs717iSdQj8zT+6BFI3G+DPOKkw+U9INTgTADTw+8f2JpqLGzuTFb7N2XhTZCqHcTHn/sFp530+8W0oUtQzGkYBIExSYEw9QJhO4B5zSFjl6xAghxGWIB7CHfgB2asSfSl0VaGqr3soyZEAi8NSvCUaWJzX/fluMc/vudJKBoNCj2DYK77W2yX' >/tmp/ssh_public_key.txt
# add public-key
openstack --insecure keypair create --public-key /tmp/ssh_public_key.txt mykey

# !!! vm
network_name="n2"
n=3

netID=$(openstack --insecure network list | grep -E "\b${network_name}\b" | awk '{ print $2 }')

for ((i = 1; i <= $n; i++)); do
  # security-group is necessary, otherwise, the instance will not be able to access internet/gateway_ip
  openstack --insecure server create --flavor m1.tiny --image cirros-0.6.3 --security-group ${security_group_name} --nic net-id=$netID --key-name mykey cirros${i}
done

network_name="e1"
netID=$(openstack --insecure network list | grep -E "\b${network_name}\b" | awk '{ print $2 }')
openstack --insecure server create --flavor m1.tiny --image cirros-0.6.3 --security-group ${security_group_name} --nic net-id=$netID --key-name mykey cirros$((n + 1))
wait

openstack server add floating ip cirros1 192.168.5.201

openstack --insecure server list

openstack console url show cirros1
# openstack console url show cirros2
# openstack console url show cirros3
# openstack console url show cirros4

# openstack compute service set --disable --up openstack82 nova-compute
# openstack compute service set --disable --up openstack84 nova-compute
